package com.leaf.spring.y201907.day22.rabbitmq;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import com.rabbitmq.client.AMQP.BasicProperties;

public class Consumer {

	public static void getMessage() throws Exception {
		Connection connection = ConnectionUtil.getConnection();
		Channel channel = connection.createChannel();
		DefaultConsumer consumer = new DefaultConsumer(channel) {
			@Override
			public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body)
					throws IOException {
				System.out.println(new String(body, StandardCharsets.UTF_8));
			}
		};
		channel.basicConsume(ConnectionUtil.QUEUE_NAME, consumer);
	}
	
	public static void main(String[] args) throws Exception {
		Consumer consumer = new Consumer();
		consumer.getMessage();
	}

}
